package com.xl98.controller;

import com.alibaba.fastjson.JSONObject;
import com.xl98.entity.ResponseCode;
import com.xl98.entity.Result;
import com.xl98.entity.ResultFactory;
import com.xl98.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ls
 * @since 2020-05-15
 */
@RestController
@Api(value = "文章",tags = {"文章"})
@RequestMapping("/api/article")
public class ArticleController {
    @Autowired
private ArticleService articleService;
    //图片上传问题
    @CrossOrigin
    @RequestMapping("/editor/upload")
    @ApiOperation(value = "上传文件")
    @ResponseBody
    public JSONObject fileUpload(@RequestParam(value="myFileName") MultipartFile file, HttpServletRequest request) throws IOException, IOException {
        //上传路径保存设置
        String separator = System.getProperty("file.separator");
        separator=separator.replaceAll("\\\\","/");
        String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() +request.getContextPath()+ separator; //获取项目路径+端口号 比如：http://localhost:8080/
        //获得SpringBoot当前项目的路径：System.getProperty("user.dir")
        String path = System.getProperty("user.dir")+"/upload/";

        //按照月份进行分类：
        Calendar instance = Calendar.getInstance();
        String month = (instance.get(Calendar.MONTH) + 1)+"月";
        path = path+month;

        File realPath = new File(path);
        if (!realPath.exists()){
            realPath.mkdir();
        }
        //上传文件地址
        System.out.println("上传文件保存地址："+realPath);
        String filename = "ks-"+ UUID.randomUUID().toString().replaceAll("-", "");

        file.transferTo(new File(realPath +"/"+ filename));
        //给WangEditor进行回调
        JSONObject res = new JSONObject();
        res.put("data",basePath+"upload/"+month+"/"+ filename);
        System.out.println(basePath+"upload/"+month+"/"+ filename);
        res.put("success", 1);
        res.put("message", "upload success!");
        return res;
    }

    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/count", method = RequestMethod.POST)
    @ApiOperation(value = "点赞或踩")
    public Result updateCount (@RequestBody Map<String, String> map) {
        int rs = articleService.updateCount(map);
        if(rs>0){
            return ResultFactory.buildSuccessResult(ResponseCode.SUCCESS_UPDATE);
        }
       else return ResultFactory.buildFailResult(ResponseCode.FAIL_UPDATE);
    }
}




